package com.Team7.controller;

import com.Team7.call.UpdateFileAttrCall;
import com.Team7.domain.SearchedFile;
import com.Team7.factory.DiskFactory;
import com.Team7.util.DiskUtil;
import com.Team7.util.FileUtil;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.stage.Stage;

import java.io.IOException;

public class FileAttrController
{
    private String name;
    private String path;
    @FXML
    private Button confirmButton;
    @FXML
    private Label fileName;
    @FXML
    private Label attribute;
    @FXML
    private Label startBlock;
    @FXML
    private Label fileType;
    @FXML
    private Label Len;
    @FXML
    private CheckBox SystemFile;
    @FXML
    private CheckBox ReadOnly;
    private Stage stage;
    private UpdateFileAttrCall updateFileAttrCall;
    public FileAttrController(UpdateFileAttrCall updateFileAttrCall, String path, String name)
    {
        this.path=path;
        this.name=name;
        this.updateFileAttrCall=updateFileAttrCall;
        this.stage=new Stage();
        FXMLLoader fxmlLoader=new FXMLLoader(getClass().getResource("../fxml/FileAttr.fxml"));
        fxmlLoader.setController(this);
        stage.setResizable(false);
        stage.sizeToScene();
        try
        {
            stage.setScene(new Scene(fxmlLoader.load()));
            stage.setTitle("文件属性");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    public void show(){
        stage.show();
    }

    @FXML
    private void initialize()
    {
        byte [][] disk=DiskFactory.getDisk();
        SearchedFile searchedFile=FileUtil.findPathSearchedFile(disk,path+name);
        fileName.setText(name);
        fileType.setText("txt");
        attribute.setText("文件");
        startBlock.setText(""+disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+6]);
        Len.setText(""+disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+7]);
        System.out.println(searchedFile.getDiskNumber());
        System.out.println(searchedFile.getDiskLocation());
        System.out.println(searchedFile.toString());
        byte check1= (byte) (disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+5]&1);
        byte check2= (byte) (disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+5]&2);
        if(check1!=0)
        {
            ReadOnly.setSelected(true);
        }
        if(check2!=0)
        {
            SystemFile.setSelected(true);
        }
        SystemFile.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event)
            {
                if(SystemFile.isSelected())
                {
                    ReadOnly.setSelected(true);

                }
                else if(!SystemFile.isSelected())
                {
                    ReadOnly.setSelected(false);
                }
            }
        });
        confirmButton.setOnAction(new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                if(SystemFile.isSelected())
                {
                    disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+5]|=3;
                }
                else
                {
                    disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+5]&=13;
                    if(ReadOnly.isSelected())
                    {
                        disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+5]|=1;
                    }
                    else
                    {
                        disk[searchedFile.getDiskNumber()][searchedFile.getDiskLocation()*8+5]&=14;
                    }
                }

                stage.close();
            }
        });

    }
}
